//
//  PanelInstallAlertViewController.swift
//  ezyform
//
//  Created by MoennigQ on 15/1/20.
//  Copyright © 2020 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit

class PanelInstallAlertViewController: BaseViewController {

    let customView = PanelInstallAlertView()
    var continueClosure: ((String)->Void)?
    var cacheDateOfCompletion: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view = self.customView
        self.customView.setTitleInfo(R.string.localizable.panelInstalledAndCommissioning())
        
        self.customView.cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: UIControl.Event.touchUpInside)
        self.customView.continueBtn.addTarget(self, action: #selector(continueBtnClick), for: UIControl.Event.touchUpInside)
        self.customView.calenderBtn.tag = 0
        let escImageList = self.getImageList()
        
        self.customView.setData(JobDetailModelHelper.shared.jobDetailModel, date: cacheDateOfCompletion,list: escImageList)

        self.customView.escImageViewTapBtnClosure = {[weak self] (rowIndex) in
            guard let strongSelf = self else {
                return
            }
            let imageList = strongSelf.getImageList()
            if imageList.count > 0{
                let vc = BrowseImageViewController()
                vc.imagesArray = imageList
                vc.currentIndex = rowIndex
                strongSelf.navigationController?.pushViewController(vc,animated: true)
            }
        }
    }
    
    fileprivate func getImageList()->[BrowseImageData]{
        var list = [BrowseImageData]()
        if let checklist = JobDetailModelHelper.shared.jobDetailModel.pvd_checklists.first(where: {$0.id == ChecklistFixedId.certificateOfElectricalSafety.rawValue}){
            _ = checklist.images.map({ iModel in
                list.append(BrowseImageData(image: iModel.image))
            })
        }
        return list
    }
    @objc
    func cancelBtnClick(){
        self.navigationController?.popViewController(animated: false)
    }
    
    @objc
    func continueBtnClick(){
        if let dateStr = self.customView.dataOfCompletionTextfield.textField.text,  dateStr != ""{
            self.continueClosure?(dateStr)
            self.navigationController?.popViewController(animated: false)
        }else{
            self.customView.dataOfCompletionTextfield.checkTextIfValid()
            self.customView.scrollView.scrollToTop(animated: true)
        }
        
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
